$(function() {
    let form = layui.form
    let layer = layui.layer
    form.verify({
        pwd: [
            /^[\S]{6,12}$/, '密码必须6到12位，且不能出现空格'
        ],
        newpwd: function(value) {
            let oldpwd = $('[name=old_pwd]').val()
            if (value === oldpwd) {
                return '新旧密码不能相同！'
            }
        },
        repwd: function(value) {
            let newpwd = $('[name=new_pwd]').val()
            if (value !== newpwd) {
                return '两次密码输入不一致！'
            }
        }
    })
    $('#resetPwd').on('submit', function(e) {
        e.preventDefault()
        $.ajax({
            method: 'PATCH',
            url: '/my/updatepwd',
            data: $(this).serialize(),
            success: function(res) {
                if (res.code !== 0) {
                    return layer.msg('密码修改失败！')
                }
                layer.msg('密码修改成功！')
                $('#resetPwd')[0].reset()
            }
        })
    })
})